package com.xiaoKe.zhonghzu;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;
import androidx.recyclerview.widget.GridLayoutManager;
import com.xiaoKe.zhonghzu.databinding.ActivityMainBinding;
import com.xiaoKe.zhonghzu.global.base.BaseActivity;
import com.xiaoKe.zhonghzu.global.bean.MainBean;
import com.xiaoKe.zhonghzu.global.tool.SPUtils;
import com.xiaoKe.zhonghzu.ui.fzxz.Gather1Fragment;
import com.xiaoKe.zhonghzu.ui.mine.MineFragment;
import com.xiaoKe.zhonghzu.ui.onClick.OnClickListenerPositionInt;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName : xiaoke-pad-zhongzhu
 * @Author : Shijingna
 * @Time : 2024/7/15 16:53
 * @Description : 主页面
 */
public class MainActivity extends BaseActivity<ActivityMainBinding> {
    List<MainBean> list = new ArrayList<>();
    int index;

    @Override
    protected void initView() {
        SPUtils.putBoolean("gpioInputData", false);//初始化云管    119离线状态
        SPUtils.putBoolean("baseActivityToOff119Fragment", true); //初始化熄屏页面

        list.add(new MainBean(R.mipmap.ic_gather1, R.mipmap.ic_gather0, "采集", "gather"));
//        list.add(new MainBean(R.mipmap.ic_data1, R.mipmap.ic_data0, "数据", "data"));
        list.add(new MainBean(R.mipmap.ic_mine1, R.mipmap.ic_mine0, "我的", "mine"));

        switchFragment(new Gather1Fragment());
        binding.recyclerViewMain.setLayoutManager(new GridLayoutManager(this, 2));
        MainActivityAdapter mainActivityAdapter = new MainActivityAdapter(list);
        mainActivityAdapter.setOnClickListenerPositionInt(new OnClickListenerPositionInt() {
            @Override
            public void onItemClick(int item) {
                if (item == index) {
                    return;
                }
                switch (list.get(item).getType()) {
                    case "gather":
//                        switchFragment(new GatherFragment());
                        switchFragment(new Gather1Fragment());
                        break;
                    case "data":
//                        switchFragment(new DataFragment());
                        break;
                    case "mine":
                        switchFragment(new MineFragment());
                        break;
                }
                index = item;
            }
        });
        binding.recyclerViewMain.setAdapter(mainActivityAdapter);

    }

    @Override
    protected void initData() {

    }


    //正确的做法,切换fragment
    private void switchFragment(Fragment ob) {
        Fragment targetFragment = (Fragment) ob;
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
        transaction.replace(R.id.nav_host_fragment_activity_main, targetFragment).commit();
    }

}